package cn.tedu.knows.search.kafka;

import cn.tedu.knows.commons.model.Topic;
import cn.tedu.knows.search.repository.QuestionRepository;
import cn.tedu.knows.search.service.IQuestionService;
import cn.tedu.knows.search.vo.QuestionVo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class KafkaConsumer {

    private Gson gson=new Gson();

    //应该找业务逻辑层对象来进行新增
    @Resource
    private IQuestionService questionService;

    @KafkaListener(topics = Topic.QUESTION_TOPIC)
    public void receiveQuestion(
            ConsumerRecord<String,String> record) {
        //获得kafka中信息内容
        String json=record.value();
        QuestionVo questionVo=gson.fromJson(json,QuestionVo.class);
        log.debug("Kafka中获得QuestionVo:{}",questionVo);
        //新增问题到ES
        questionService.saveQuestion(questionVo);
    }



}
